<template>
	<view class="content">
		<view class="school">
			<!-- <span>宿舍号：</span> -->
			<label for="rn">校区：</label>
			<block v-if="haveSchool && schoolArr.length > 0">
				<picker mode="selector" :range="schoolArr" :value="school_index" @change="chooseSchool">
					<view>{{schoolArr[school_index]}}</view>
				</picker>
			</block>
			<block v-else>
				<input type="text" placeholder="3到6位字母，中文" v-model="schoolName">
				<span v-if="schoolArr.length > 0" class="iconfont icon-fanhui" @click="backSchool"></span>
			</block>
		</view>
		<view class="roomnum">
			<!-- <span>宿舍号：</span> -->
			<label for="rn">宿舍号：</label>
			<input type="text" placeholder="3到6位字母，数字" v-model="roomNum">
		</view>
		<view class="roomtype">
			<label for="rt">类型：</label>
			<radio-group id="rt" @change="changetype">
				<radio value='1' checked>男</radio>
				&nbsp;&nbsp;&nbsp;
				<radio value='2'>女</radio>
			</radio-group>
		</view>
		<view class="members" v-for="(name,index) in membersArray" :key="index">
			<label for="mb">成员{{index+1}}：</label>
			<input type="text" placeholder="3到6位字母，中文" v-model="membersArray[index]">
			<span v-if="index == 0 && membersArray.length <= 9" class="iconfont icon-jiahao jia"
				@click="addMember"></span>
		</view>
		<view class="floor">
			<label for="fl">楼层：</label>
			<picker mode="selector" @change="changeFl" :value="index" :range="flarr">
				<view>{{flarr[index]}}</view>
			</picker>
		</view>
		<view class="btns" v-if="!setRoom">
			<button type="warn" @click="reset">重置</button>
			<button type="primary" @click="commit">提交</button>
		</view>
		<view class="btns" v-else>
			<button type="default" @click="delRoom">删除</button>
			<button type="primary" @click="setRoomCommit">修改</button>
		</view>
	</view>
</template>

<script>
	import doErro from '../../tools/doErro/index.js'
	import canYouCommit from '../../tools/canYouCommit/index.js'
	import checkALL from "../../tools/checkToken/index.js";
	export default {
		onLoad(option) {
			//checkALL.checkToken(this.$store.state.token)
			//console.log(option);
			let that = this;
			//获取用户现有校区
			uniCloud.callFunction({
				name: "rc_room",
				data: {
					type: "getSchoolArr",
					uid: getApp().globalData.uid
				},
				success(res) {
					//console.log(res);
					let list = res.result.data;
					let newList = [];
					list.forEach((item, index) => {
						newList[index] = item.school;
					});
					newList = Array.from(new Set(newList));
					newList.forEach((item) => {
						that.schoolArr.push(item);
					})
					that.schoolArr.push("新增校区");
					if (that.schoolArr[0] == "新增校区" && that.schoolArr.length == 1) {
						that.haveSchool = false;
					}
				},
				fail(err) {
					doErro.showErro(500);
					uni.hideLoading();
				}
			});

			//设置标记，切换成修改宿舍的ui
			if (option.method == "fix") {
				this.setRoom = 1;
				this.roomId = option.roomId;
				//根据roomid查询宿舍
				uniCloud.callFunction({
					name: "rc_room",
					data: {
						type: "getOneRoom",
						roomId: option.roomId
					},
					success(res) {
						that.schoolName = res.result.data[0].school;
						that.roomNum = res.result.data[0].roomNum;
						that.roomtype = res.result.data[0].roomtype;
						that.membersArray = res.result.data[0].members;
						that.index = res.result.data[0].floor;
					},
					fail(err) {
						console.log(err);
					}
				})
			}
		},
		data() {
			return {
				flarr: ["选择楼层", "1层", "2层", "3层", "4层", "5层", "6层", "7层", "8层", "9层", "10层"],
				flnum: "",
				index: 0,
				membersArray: [""],
				roomtype: "1",
				roomNum: "",
				schoolArr: [],
				school_index: 0,
				schoolName: "",
				haveSchool: true,
				//修改宿舍相关数据
				setRoom: 0,
				roomId: ""
			}
		},
		methods: {
			changetype(e) {
				this.roomtype = e.detail.value;
			},
			changeFl(e) {
				this.index = e.detail.value;
				this.flnum = this.flarr[this.index];
			},
			addMember() {
				this.membersArray.push("");
				if (this.membersArray.length == 9) {
					uni.showModal({
						content: "手机端宿舍最多添加十名宿舍成员，如需更多定制服务，请联系我们99837483@qq.com"
					})
				}
			},
			chooseSchool(e) {
				this.school_index = e.detail.value;
				this.schoolName = this.schoolArr[this.school_index];
				if (this.schoolArr.length == (Number(e.detail.value) + 1)) {
					this.haveSchool = false;
					this.schoolName = "";
				}
			},
			backSchool() {
				this.haveSchool = true;
				this.school_index = 0;
			},
			reset() {
				this.roomtype = 0;
				this.membersArray = [""];
				this.index = 0;
				this.roomNum = "";
			},
			commit() {
				if (this.schoolArr[this.school_index] != "新增校区") {
					this.schoolName = this.schoolArr[this.school_index];
				}
				//验证标记
				let res = true;
				//验证其他数据
				res = canYouCommit.doCommit({
					school: this.schoolName,
					roomNum: this.roomNum,
					roomtype: this.roomtype,
					floor: this.index
				});

				//验证成员
				this.membersArray.forEach((item, index) => {
					if (item.trim() == "") {
						doErro.showErro("members");
						res = false;
						return;
					} else if (index >= 1) {
						if (this.membersArray[0] == item) {
							doErro.showErro("membersName");
							res = false;
							return;
						}
					}
				});

				//验证宿舍号是否重复
				if (res) {
					let that = this;
					uni.showLoading({
						title: "宿舍创建中...",
						mask: true
					})
					uniCloud.callFunction({
						name: 'rc_createroom',
						data: {
							type: 'checkNum',
							school: that.schoolName,
							roomNum: that.roomNum,
							floor: that.index,
							uid: getApp().globalData.uid
						},
						success(data) {
							if (data.result.affectedDocs == 1) {
								res = false;
								doErro.showErro("roomNum2");
								uni.hideLoading();
							} else {
								uniCloud.callFunction({
									name: "rc_createroom",
									data: {
										school: that.schoolName,
										roomNum: that.roomNum,
										roomtype: that.roomtype,
										members: that.membersArray,
										floor: that.index,
										uid: getApp().globalData.uid,
										createtime: Date.parse(new Date())
									},
									success(res) {
										if (res.result.success) {
											uni.showModal({
												content: "操作成功"
											});
											getApp().globalData.isHave = 1;
											uni.hideLoading();
										}
									},
									fail(err) {
										console.log(err);
									}
								})
							}
						},
						fail(err) {
							console.log(err);
						}
					})
				}
			},
			//修改宿舍的函数
			setRoomCommit() {
				//验证标记
				let res = true;
				//验证其他数据
				res = canYouCommit.doCommit({
					school: this.schoolName,
					roomNum: this.roomNum,
					roomtype: this.roomtype,
					floor: this.index
				});

				//验证成员
				this.membersArray.forEach((item, index) => {
					if (item.trim() == "") {
						doErro.showErro("members");
						res = false;
						return;
					} else if (index >= 1) {
						if (this.membersArray[0] == item) {
							doErro.showErro("membersName");
							res = false;
							return;
						}
					}
				});
				if (res) {
					let that = this;
					uni.showLoading({
						title: "宿舍修改中...",
						mask: true
					});
					uniCloud.callFunction({
						name: "rc_createroom",
						data: {
							type: "setRoom",
							school: that.schoolName,
							roomNum: that.roomNum,
							roomtype: that.roomtype,
							members: that.membersArray,
							floor: that.index,
							roomId: that.roomId
						},
						success(res) {
							if (res.result.updated) {
								uni.showModal({
									title: "修改成功"
								});
								uni.hideLoading();
							} else {
								uni.showModal({
									title: "未做修改"
								});
								uni.hideLoading();
							}
						},
						fail(err) {
							console.log(err);
						}
					})
				}
			},
			//删除宿舍的函数
			delRoom() {
				let that = this;
				uni.showLoading({
					title: "删除宿舍中...",
					mask: true
				});
				uniCloud.callFunction({
					name: "rc_createroom",
					data: {
						type: "delRoom",
						roomId: that.roomId,
						uid: getApp().globalData.uid
					},
					success(res) {
						if (res.result.success) {
							getApp().globalData.isHave = res.result.ishave;
							uni.showModal({
								title: "删除成功"
							});
							uni.hideLoading();
							uni.switchTab({
								url: "/pages/room/room"
							})
						}
					},
					fail(err) {
						console.log(err);
					}
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.content {
		width: 100%;

		.school {
			.iconfont {
				border: 0rpx solid red;
				text-align: right;
				box-sizing: border-box;
				padding-right: 20rpx;
				line-height: 50rpx;
			}
		}

		view {
			width: 100%;
			height: 100rpx;
			border-bottom: 1rpx solid lightgray;
			display: flex;
			flex-direction: row;
			box-sizing: border-box;
			padding-left: 10rpx;
			padding-top: 35rpx;
			font-size: 38rpx;
			
			input{
				margin-top: 7rpx;
			}
			
			label {
				height: 100rpx;
				line-height:44rpx;
				width: 25%;
			}

			picker {
				position: relative;
				height: 50rpx;
				width: 200rpx;

				view {
					position: absolute;
					height: 100rpx;
					top: -35rpx;
				}
			}
		}

		.members {
			position: relative;

			.jia {
				display: inline-block;
				position: absolute;
				z-index: 10;
				left: 660rpx;
				top: 25rpx;
				height: 55rpx;
				line-height: 55rpx;
				width: 55rpx;
				border: 1rpx solid green;
				border-radius: 50%;
				font-size: 38rpx;
				color: green;
				text-align: center;
			}
		}

		.btns {
			height: 200rpx;
			display: flex;
			flex-direction: column;
			padding: 0rpx;
			padding-top: 10rpx;

			button {
				height: 200rpx;
				line-height: 100rpx;
			}
		}

	}
</style>